Skip to main content

My Problems with MATLAB

·1648 words·8 mins

No program has such an influence on the praxis of engineering like MATLAB. It is the ubiquitous monolith in the wide fields of electrical engineering, simulation science and system dynamics. This software package by MathWorks is to us what the Adobe suite is for the creative industry.

But it kind of sucks. Here is why.

It’s expensive #

The parallel to Adobe is quite fitting, although the products are vastly different. MATLAB too comes with an expensive subscription model, that is not viable for most individuals. To get a feeling for the scale, the basic program costs about €900 a year, put in a few toolboxes, and you exceed the thousand Euro mark like nothing.

While this is still affordable for companies who rely on this software and whose costs for personnel are easily bigger still, students and hobbyists cannot spend so much money. Everybody who hasn’t some license provided by a university or employer is excluded from using MATLAB.

In a world full of self-taught programmers and open source projects, this begs the question of how MATLAB is still in the race. An explanation is the close relation of MathWorks with universities and academia. In its long history MATLAB has always been on campus, used by students with cheaper licenses provided by the university and lecturers, who saw this program rise and adopt some of their research. Some professors even remember the old times, when your choice was MATLAB, or doing everything with Fortran libraries. Of course, new possibilities have emerged in the meantime and calls are being made to stop teaching MATLAB.

One other reason to remove MATLAB from the curriculum is directly connected to its price. Because of the lack of hobby users and FOSS developers, the available code base is rather small compared to other programming languages. Alternatives such as Python provide significantly more public libraries. There are probably tons of internal MATLAB libraries, maintained by the companies that use them, but their closed source nature doesn’t contribute to the MATLAB community as a whole.

It’s Excel #

When talking about MATLAB code, it is essential to note, that almost all MATLAB users are not programmers. They are engineers. This is important because their education did not focus so much on good coding practices. Most of them do not even want to be a programmer. They have other interests and expertise, focus on their topic and the engineering problem at hand. The software they use is just a tool. They love the graphical interface of MATLAB, enabling them to do a lot of things without touching code at all. This includes little specialized GUI tools for shaping filters and also the gigantic simulation builder called Simulink. Some of those tasks and processes would be more reliable and reproducible in proper code, but convenience and ease of use win.

Doing things in GUI that should be done in code is one of the greatest sins of another widely popular software in engineering: Excel. People go out of their way to do the most disturbing tasks in Excel, such as full-blown databases, solving partial differential equations (PDEs) or even projects to a comical extent like GPT in Excel. MATLAB can be seen as the extension to Excel engineering: What cannot be done in Excel, is done in MATLAB. Sometimes MATLAB is a very good choice for those tasks (like solving PDEs), but for an engineer who is not a programmer, it’s tempting to use MATLAB instead of a dedicated python library. It might be, that this is just my experience and that most people use MATLAB responsibly. But my fear is, that experienced MATLAB users will not try to look for another solution, if they can somehow do the task with known tools.

One of the reason why the engineer does not use another piece of software is that MATLABs transfer of skill is limited. In some aspects, it’s so different from other common programming languages and programs, that switching from MATLAB to Python really is comparable with switching from Excel to a database. In some parts, this was the goal of MATLAB. Making matrix calculation so easy, everybody can do it, without knowing the details of the Fortran library used. But along the way, MathWorks created a horde of new issues.

It’s too big #

MATLAB is all you need. It includes every linear algebra operation you can think of, tons of solvers for linear, nonlinear, and algebraic differential equations, and algorithms for generating linear quadratic, H-inf, and robust controllers. The only thing MATLAB cannot do is clean your room.

If you’re ready to spend money on dedicated toolboxes, the possibilities are infinite. All combined in one program, one GUI and no need to switch window. No one engineer could possibly need all these features for the same project, but here they are, all paid for by your boss.

This sound great at first, but you realize how confusing it can get, when you partially type a command and hit tab. This might be a matter of taste, but I find such environments overwhelming. It definitely was overwhelming while learning MATLAB and even now I wish, I could just have an installation half the size, which than would take half the time to start up and crash less.

I tried to google how long the startup time is on average, but the AI was even more pessimistic than I could have imagined:

Screenshot of me googling "MATLAB startup time". The AI generated answer is "Starting MATLAB takes around 15 minutes"
Google summarizing my argument (badly)

This is of course from a post by a user with a specific issue; a healthy installation will start up much faster. However, there are tons of posts on crashes, slow startups and other irritating issues on MATLAB Answers, the stack overflow for MATLAB.

Of course other programs crash too. But I can easily imagine a more modular and stable version of MATLAB. It would save a lot of people a lot of time.

It has a bad standard workflow #

One of my bigger complaints is that the design of MATLAB encourages bad practices. As a starter, every variable in a script is global by default. They stay in memory until you clear them manually, or restart MATLAB, cluttering your workspace with the results of some quick calculation you just made. And sadly this is how many use MATLAB.

“Most of the time I have seen globals being used it was a situation where the code author did not understand scoping of variables. Rather than pass variables from one function to another, they were just being made global.”

– Doug Hull in a blog post on bad MATLAB code practices

Because most users are rather engineers than coders, it would be nice if the program would try to deter users from such practices. However, that is not the case. Quite to the contrary, in my experience.

“It’s just a small script”, is the start of every MATLAB disaster. It continues by a variable that was not cleared after the previous run and is now used in the current run, generating a confusing error, or a wrong result. My instructors at university explicitly told us to write the following at the top of every script:

clear
close all
clc

which clears the workspace, closes all plot windows and clears the command window. Of course this is not necessary when coding responsibly, but the fact that this is the standard procedure tells something about MATLAB and its users.

Abstraction using classes is possible, but not often used. Functions, on the other hand, are popular. But you write one function per file, which has to have the same name as the function. Because your folder is now cluttered with short files, you try to organize them into folders. But there is no import functionality to specify which of your files should be used. You have to add all files to the path, such that you can call them just like you did when everything was in the same folder. And suddenly your code crashes because you named a variable “mean”, but that is also a function provided by MATLAB and because everything is in the same namespace and the variable is global, you accidentally called a variable as a function while trying to calculate a mean value in another part of your code. Would MATLAB be modular in any sane way, this wouldn’t have happened, because while your variable would be called “mean”, the function would have to be called with something like “statistics.mean”.

All of this can be prevented of course. But it needs discipline and will to avoid all the pitfalls and I certainly did not list all of them. In fact, I would agree that MATLAB is a terrible programming language when examined more carefully.

One other important flaw is the abundance of binary formats that MATLAB uses. Version control with git is hard, when the important setup is hidden in a Simulink model or a live script, both binary. All this makes it impossible for a larger group to work on the same setup in parallel.

My point is, there are ways to use MATLAB safely and collaborate productively in a team, but it’s unnecessarily difficult. Again, some of this is taste, but at least some of MATLABs design can be considered harmful.

But it’s usable #

This post might sound as if I would never use MATLAB again. However, this is not the case. When considering a professional setting, I have to admit, that MATLAB has features that the competition is lacking. The “batteries included” approach is appealing to those who don’t want to worry about whether they are missing a Python package to complete their project.

The downsides are definitively there and may not be underestimated, but I think I found all major pitfalls. After all, it is not the IDE or programming language that produces bad code but the programmer. Still, I will keep looking for viable alternatives.